package Search.search2020;

/**
 * 有序数组的二分查找
 * @author: leiwenkang
 * @date: 2020/5/23
 * @time: 18:12
 */
public class BinarySearch2 {
    public int binarySearch(int[] array, int target) {
        if (array == null || array.length == 0) {
            return -1;
        }

        int low = 0;
        int high = array.length - 1;

        while (low <= high) {
            int mid = (low + high) >> 1;
            if (array[mid] == target) {
                return mid;
            } else if (array[mid] > target) {
                high = mid - 1;
            } else {
                low = mid + 1;
            }
        }
        return -1;
    }
}
